package com.wzx.selfuse.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wzx.selfuse.entity.PlanCust;
import com.wzx.selfuse.entity.PlanPro;
import com.wzx.selfuse.service.impl.Plan_ICustServiceImpl;
import com.wzx.selfuse.service.impl.Plan_IProServiceImpl;
import com.wzx.selfuse.vo.AjaxEntityResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Liu Tenda
 * @date 2022年 12月11日 16:27:17
 * Introduction:
 */
@Controller
@RequestMapping("/cust")
public class Plan_CustController {
    @Autowired
    Plan_ICustServiceImpl plan_iCustService;

    //    产品列表
    @RequestMapping("/custList")
    public String getCustList(HttpServletRequest req){
        Integer page=1;
        Integer num=5;
        if (req.getParameter("page")!=null){
            page = Integer.parseInt(req.getParameter("page"));
        }
        PageHelper.startPage(page,num);
        List<PlanCust> custList = plan_iCustService.getCustList();
        PageInfo<PlanCust> of=PageInfo.of(custList);
        req.setAttribute("custList",of.getList());
        req.setAttribute("custListOf",of);
        return "plan_cust";
    }

    //    跳转添加产品页面
    @RequestMapping("/plan_cust_add")
    public String custAdd(){

        return "plan_cust_add";
    }

    //    添加产品
    @PostMapping("/addCust")
    @ResponseBody
    public AjaxEntityResult addCust(@RequestBody PlanCust planCust){
        AjaxEntityResult ajaxEntityResult = new AjaxEntityResult();
        PlanCust custByCustNumber = plan_iCustService.getCustByCustNumber(planCust.getCustNumber());
        if (custByCustNumber!=null) {
            ajaxEntityResult.setCode(400);
            ajaxEntityResult.setMsg("编号已存在");
        } else {
            ajaxEntityResult.setCode(200);
            ajaxEntityResult.setMsg("添加成功");
            plan_iCustService.addCust(planCust);
        }
        return ajaxEntityResult;
    }

    //    删除产品
    @PostMapping("/deleteCust")
    @ResponseBody
    public AjaxEntityResult deleteCust(@RequestBody String custNumber){
        int i = plan_iCustService.deleteCust(custNumber);
        AjaxEntityResult ajaxEntityResult=new AjaxEntityResult();
        if (i==0){
            ajaxEntityResult.setCode(400);
            ajaxEntityResult.setMsg("删除时发生错误");
        }
        if (i==1){
            ajaxEntityResult.setCode(200);
            ajaxEntityResult.setMsg("删除成功");
        }
        return ajaxEntityResult;
    }

    //    删除选中
    @PostMapping("/deleteChecked")
    @ResponseBody
    public AjaxEntityResult deleteChecked(@RequestBody List<String> custNumber){
        Iterator<String> iterator = custNumber.iterator();
        int a=0;//用于计数批量删除了多少条
        while (iterator.hasNext()){
            String next = iterator.next();
            int i = plan_iCustService.deleteCust(next);
            if (i==1){
                a++;
            }
        }
        AjaxEntityResult ajaxEntityResult=new AjaxEntityResult();
        if (a>0){
            ajaxEntityResult.setCode(200);
            ajaxEntityResult.setMsg("成功删除"+a+"条数据");
        }else {
            ajaxEntityResult.setCode(400);
            ajaxEntityResult.setMsg("删除失败，未选择用户或用户不存在");
        }
        return ajaxEntityResult;
    }

    //    跳转到产品介绍页面
    @RequestMapping("/plan_cust_introduction")
    public String custIntroduction(HttpServletRequest req){
        String custNumber = req.getParameter("custNumber");
        PlanCust cust = plan_iCustService.getCustByCustNumber(custNumber);
        String mark = cust.getMark();
        req.setAttribute("mark",mark);
        return "plan_cust_introduction";
    }

    @RequestMapping("/plan_cust_edit")
    public String custEdit(HttpServletRequest req){
        String custNumber = req.getParameter("custNumber");
        PlanCust cust = plan_iCustService.getCustByCustNumber(custNumber);
        req.setAttribute("cust",cust);
        return "plan_cust_edit";
    }

    @RequestMapping("/editCust")
    @ResponseBody
    public AjaxEntityResult editCust(@RequestBody PlanCust planCust){
        int i = plan_iCustService.editCust(planCust);
        AjaxEntityResult result=new AjaxEntityResult();
        if (i==0){
            result.setCode(400);
            result.setMsg("修改失败");
        }
        if (i==1){
            result.setCode(200);
            result.setMsg("修改成功");
        }
        return result;
    }

    @RequestMapping("/checkCust")
    public String checkCust(String controller,String searchCust,HttpServletRequest req){
        List<PlanCust> cust=new ArrayList<>();
        switch (controller){
            case "1":
                PlanCust checkedCust = plan_iCustService.getCustByCustNumber(searchCust);
                cust.add(checkedCust);
                break;
            case "2":
                cust = plan_iCustService.getCustByCustName(searchCust);
                break;
            default:
                req.setAttribute("msg","找不到符合条件的产品");
        }
        req.setAttribute("custList",cust);
        return "plan_cust";
    }
}